<?php
$allArticleCategory = $this->allArticleCategory;
$allSubArticleCategory = $this->allSubArticleCategory;

?>

<div class="ad_title_ctn">
    <h2>Quản lý tin bài</h2>
</div>

<div class="ad_content_ctn">
    <div class="article_critetia_ctn">
        <div class="left" style="width:300px">
            <span>Nhóm chính:</span>
            <select id="cboPrimaryCategory">
                <option value="ALL">All</option>
                <?php
                foreach ($allArticleCategory as $aCategory) {
                    echo '  <option value="'.$aCategory->id.'">'.$aCategory->name.'</option>';
                }

                ?>
            </select>
        </div>
        <div class="left" style="width:300px">
            <span>Nhóm phụ:</span>
            <select id="cboSubCategory">
                <option value="ALL">All</option>
                <?php

                foreach ($allSubArticleCategory as $aCategory) {
                    echo '  <option value="'.$aCategory->id.'">'.$aCategory->name.'</option>';
                }
                ?>
            </select>
        </div>

        <div class="clear"></div>
        <div style="margin: 10px 0px">
            <span>Tiêu đề bài viết: </span>
            <input class="k-textbox" style="width: 300px" id="txtHeader" >
        </div>

    </div>
    <div class="article_search_ctr">
        <a id="btnSearch"  class="k-button k-button-icontext">Tìm kiếm</a>
    </div>
    <div id="article_grid"></div>
    <div id="details"></div>
</div>

<script type="text/x-kendo-template" id="template">
    <div class="toolbar">
        <a class="k-button k-button-icontext  k-grid-edit"
           href="/admin/article/add">
            <span class="k-icon k-add"></span>Tạo mới
        </a>
    </div>
</script>

<script type="text/x-kendo-template" id="detailTemplate">
    <div id="details-container">
        <h3>#= header #</h3>
        <em>#= sumarize #</em>
        <dl>
            <dt>Nhóm chính #= primary_category #</dt>
            <dt>Nhóm phụ   #= sub_category #</dt>
            <dt>Tag        #= tag #</dt>
        </dl>
    </div>
</script>

<script type="text/javascript">
    var wnd,
            detailsTemplate;
    $(document).ready(function() {
        $(".article_critetia_ctn").keyup( function(event) {
            if ( event.which == 13 ) {
                event.preventDefault();
                doSearch();
            }
        });

        $("#cboPrimaryCategory").kendoComboBox({
            filter: "contains",
            suggest: true,
            index: 3
        });

        var cboPrimaryCategory = $("#cboPrimaryCategory").data("kendoComboBox");
        $("#cboSubCategory").kendoComboBox();
        var cboSubCategory = $("#cboSubCategory").data("kendoComboBox");

        dataSource = new kendo.data.DataSource({
            type: "jsonp",
            transport: {

                read: function(options) {
                    options.data.name=$('#txtHeader').val();
                    options.data.category=cboPrimaryCategory.value();
                    options.data.subCategory=cboSubCategory.value();

                    $.ajax( {
                        url: "<?php echo '/admin/article/list'  ?>",
                        data: options.data, // the "data" field contains paging, sorting, filtering and grouping data
                        success: function(result) {
                            options.success(result);
                        },
                        dataType: "json",
                        type: "POST"

                    });
                },

                destroy: function(options) {
                    $.ajax( {
                        url: "/admin/article/remove?id="+options.data.id,
                        data: options.data,
                        success: function(result) {
                            if(result.success){
                                options.success(options.data.models);
                                $('#error_ctn').addClass('blue_text').html('Xóa thành công.');
                            }else{
                                $('#error_ctn').removeClass('blue_text').html(result.msg);
                                grid = $("#article_grid").data("kendoGrid");
                                // cancel changes
                                grid.cancelChanges();
                            }
                        },
                        dataType: "json",
                        type: "POST"
                    });
                }
            },
            schema: {
                model: {
                    id: "id",
                    fields: {
                        id: {editable: false,  type: "number" },
                        header: { type: "string" },
                        user_name: { type: "string" },
                        order_number: { type: "number" },
                        active: { type: "boolean" },
                        created_date: { type: "date" }
                    }
                },
                data: "rows",
                total: "total"
            },
//                    batch: true,
            pageSize: 30,
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        });

        $("#article_grid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable: true,

            columnMenu: true,
            sortable: true,
            resizable:true,
            toolbar:kendo.template($("#template").html()),

            columns: [
                { field: "header", title: "Tiêu đề", width: 300, template:" <a href='<?php echo '/admin/article/add?id='  ?>#= id #'>#= header #</a> " },
                { field: "user_name", title: "Người tạo", width: 50 },
                { field: "created_date", title: "Ngày tạo", width: 50,
                    template: '# if(created_date != null){#' +
                            '#= kendo.toString(created_date,"dd/MM/yyyy") #' +
                            '#}#'},

                { field: "active", title: "Active", width: 40 },
                { field: "order_number", title: "Số thứ tự", width: 50 },

                { command:  [{name: "destroy", text: ""},
                    { text: "Chi tiết", click: showDetails }],  title: "&nbsp;", width: 80 }

            ],
            editable: "inline"
        });

        wnd = $("#details")
                .kendoWindow({
                    title: "Thông tin bài viết",
                    modal: true,
                    visible: false,
                    resizable: false,
                    width: 500
                }).data("kendoWindow");

        detailsTemplate = kendo.template($("#detailTemplate").html());
         function showDetails(e) {
            e.preventDefault();
              dataItem = this.dataItem($(e.currentTarget).closest("tr"));
             id = dataItem.id;
             $.post('/admin/article/get-article-for-view',{id:id}
                     ,function(result){
                         if (result.success){

                              newDataItem ={header:result.header,
                                 sumarize:result.sumarize,
                                 primary_category:result.primary_category,
                                 sub_category:result.sub_category,
                              tag:result.tag};

                             wnd.content(detailsTemplate(newDataItem));
                             wnd.title(result.header);
                             wnd.center().open();

                         }else{

                         }

                     },'json');


        }

        function doSearch(){
            grid = $("#article_grid").data("kendoGrid");
            grid.dataSource.page(1);
            grid.dataSource.read();
        }

        $('#btnSearch').click(function(){
            doSearch();
        })
    });

</script>